<template>
  <el-dialog
    :model-value="visible"
    title="介质关联媒资"
    :before-close="cancelDoRelateDialog"
    :top="doRelateDataTmp.hasseries ? '8vh' : '15vh'"
  >
    <el-row>
      <el-col :span="24">
        <el-card>
          <template #header>关联节目集</template>
          <div style="display: flex;justify-content: space-between;align-items: center">
            <div>选择：
              <el-checkbox v-model="doRelateDataTmp.media_checked"
                           :label="doRelateDataTmp.mediaid"
                           :disabled="!!(doRelateDataTmp.videoid && doRelateDataTmp.hasseries)"
              >
                {{''}}
              </el-checkbox>
            </div>
            <div>媒资ID：{{doRelateDataTmp.mediaid}}</div>
            <div>媒资名称：{{doRelateDataTmp.medianame}}</div>
          </div>
        </el-card>

      </el-col>
      <el-col :span="24" v-if="doRelateDataTmp.hasseries" style="margin-top: 10px">
        <el-card>
          <template #header>关联子集</template>
          <el-row>
            <el-col :span="24" style="margin-bottom: 10px">
              <el-input placeholder="子集名称" clearable v-model="seriesQueryListTmp.keyword" style="width: 50%">
                <template #append>
                  <el-button @click="reloadSeriesList">
                    <el-icon>
                      <search />
                    </el-icon>
                    搜索
                  </el-button>
                </template>
              </el-input>
            </el-col>
            <el-col :span="24">
              <el-table ref="seriesTableRef" :data="doRelateDataTmp.seriesList" border max-height="250px"
                        @select="handlerSelect"
                        @select-all="handlerSelectAll"
                        @row-click="handleClickRowSelect"
              >
                <el-table-column type="selection" width="80" />
                <el-table-column label="子集ID" prop="seriesid" width="100"/>
                <el-table-column label="子集名称" prop="name" :show-overflow-tooltip="true" />
                <el-table-column label="子集集号" prop="episodenum" width="100"/>
                <el-table-column label="最后更新时间" prop="ctime" />
              </el-table>
            </el-col>
            <el-col :span="24" style="margin-top: 10px">
              <table-pagination
                      :queryList="seriesQueryListTmp"
                      :total="doRelateDataTmp.seriesTotal"
                      @pagination="reloadSeriesList"
              />
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
    <template #footer>
      <div align="center">
        <el-button type="default" size="small" @click="cancelDoRelateDialog">取消</el-button>
        <el-button type="primary" size="small" @click="handleConfirm">确定关联</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
  import {toRef, ref} from 'vue'
  import { Search } from "@element-plus/icons"
  import TablePagination from "@/views/public/TablePagination";
  import {imageRelMedia} from "@/api/media/image";
  import {ElMessage} from 'element-plus'
  import {videoRelMedia} from "@/api/media/video";

  export default {
    name: "DoRelate",
    components: {Search, TablePagination},
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      dataTmp: {
        type: Object
      },
      seriesQueryList: {
        type: Object
      }
    },
    emits: ['cancelDoRelateDialog', 'reloadSeriesList', 'cancelRelateDialog'],
    setup(props, context) {
      const doRelateDataTmp = toRef(props, 'dataTmp')
      const seriesQueryListTmp = toRef(props, 'seriesQueryList')
      const seriesTableRef = ref(null)

      const cancelDoRelateDialog = () => {
        doRelateDataTmp.value.media_checked = false
        context.emit('cancelDoRelateDialog', seriesQueryListTmp)
      }

      const reloadSeriesList = () => {
        context.emit('reloadSeriesList')
      }
      const handleConfirm = () => {
        if (doRelateDataTmp.value.imgid) {
          imageRelMedia(doRelateDataTmp.value).then(response => {
            if (response.errcode === 0) {
              ElMessage({type: 'success', message: response.msg, duration: 2000})
              cancelDoRelateDialog()
              context.emit('cancelRelateDialog')
            } else {
              ElMessage({type: 'error', message: response.msg, duration: 2000})
            }
          })
        } else {
          videoRelMedia(doRelateDataTmp.value).then(response => {
            if (response.errcode === 0) {
              ElMessage({type: 'success', message: response.msg, duration: 2000})
              cancelDoRelateDialog()
              context.emit('cancelRelateDialog')
            } else {
              ElMessage({type: 'error', message: response.msg, duration: 2000})
            }
          })
        }
      }
      const handlerSelect = (selection, row) => {
        if (doRelateDataTmp.value.seriesid_list.includes(row.seriesid)) {
          const index = doRelateDataTmp.value.seriesid_list.findIndex(sid => sid === row.seriesid)
          doRelateDataTmp.value.seriesid_list.splice(index, 1)
        } else {
          doRelateDataTmp.value.seriesid_list.push(row.seriesid)
        }
        doRelateDataTmp.value.series_checked = doRelateDataTmp.value.seriesid_list.length > 0
      }
      const handlerSelectAll = (selection) => {
        if (selection.length > 0) {
          selection.forEach(series => {
            if (!doRelateDataTmp.value.seriesid_list.includes(series.seriesid)) {
              doRelateDataTmp.value.seriesid_list.push(series.seriesid)
            }
          })
        } else {
          doRelateDataTmp.value.seriesList.forEach(series => {
            const index = doRelateDataTmp.value.seriesid_list.findIndex(sid => sid === series.seriesid)
            if (index !== -1) {
              doRelateDataTmp.value.seriesid_list.splice(index, 1)
            }
          })
        }
        doRelateDataTmp.value.series_checked = doRelateDataTmp.value.seriesid_list.length > 0
      }
      const handleClickRowSelect = (row) => {
        if (doRelateDataTmp.value.seriesid_list.includes(row.seriesid)) {
          const index = doRelateDataTmp.value.seriesid_list.findIndex(sid => sid === row.seriesid)
          doRelateDataTmp.value.seriesid_list.splice(index, 1)
          seriesTableRef.value.toggleRowSelection(row, false)
        } else {
          seriesTableRef.value.toggleRowSelection(row, true)
          doRelateDataTmp.value.seriesid_list.push(row.seriesid)
        }
        doRelateDataTmp.value.series_checked = doRelateDataTmp.value.seriesid_list.length > 0
      }

      return {
        doRelateDataTmp,
        cancelDoRelateDialog,
        reloadSeriesList,
        seriesQueryListTmp,
        handleConfirm,
        seriesTableRef,
        handlerSelect,
        handlerSelectAll,
        handleClickRowSelect
      }
    }
  }
</script>

<style scoped>

</style>
